<?php
namespace app\Capital\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\Capital\Shopcapital;
use think\Model;
use think\Db;
use app\Capital\model\capital as CapitalModel;
use app\Capital\model\account;
use think\Cache;
class Membercapital extends Admin
{

    /*
     *收支账务报表
     *
     *进出项求和
     *
     *
     * */
    public function index(){
            $search=input('search');
            $where=[];
            $towhere=[];
        if($search){
            $towhere['id|company|mobile']=$search;
            $member=Db::name("member")->where($towhere)->find();
            if($member){
                $where['memberid']=$member['id'];
                $this->assign('search',$search);
            }
        }
            $startDate=input('date1');
            $endDate=input('date2');
            if($startDate && $endDate){
                $this->assign('startDate',$startDate);
                $this->assign('endDate',$endDate);
                $startDate = strtotime($startDate . ' 0:00:00');
                $endDate = strtotime($endDate . " 23:59:59");
                // $where['createtime']=['between',[$startDate,$endDate]];
            }
        if($where){
            $data=Db::name('member_account')->where($where)->order('createtime')->select();
        }else{
            $data=Db::name('member_account')->order('createtime')->select();
        }
        $member=[];
        foreach($data as $value){
            if(isset($member[$value['memberid']])){
                    if(isset($member[$value['memberid']][$value['account_type']])){
                        array_push($member[$value['memberid']][$value['account_type']],$value);
                    }else{
                        $member[$value['memberid']][$value['account_type']][]=$value;
                    }

            }else{
                $member[$value['memberid']][$value['account_type']][]=$value;
            }
        }
        $account=[];
        foreach($member as $k=>$item){
            foreach($item as $kk=> $vv){
                $account[$k][$kk]=$vv;
                foreach($vv as $v){
                    if(isset($account[$k][$kk]['type'][$v['type']])){
                        $account[$k][$kk]['type'][$v['type']]['num']+=$v['num'];
                        $account[$k][$kk]['type']['closing_balance']=$v['balance']+$v['num'];
                    }else{
                        $account[$k][$kk]['type'][$v['type']]['num']=$v['num'];
                        $account[$k][$kk]['type']['closing_balance']=$v['balance']+$v['num'];
                    }
                }
            }
        }
      //  dump($account);exit();
        $this->assign('account',$account);
        return $this->fetch('member');
    }

    //会员账务详情
    public function member_accounting(){
       $memberid=input('memberid');
        $startDate=input('startDate');
        $endDate=input('endDate');
        $where=[];
        if($startDate && $endDate){
            $this->assign('startDate',$startDate);
            $this->assign('endDate',$endDate);
            $startDate = strtotime($startDate . ' 0:00:00');
            $endDate = strtotime($endDate . " 23:59:59");
            //$where['createtime']=['between',[$startDate,$endDate]];
        }
        if($memberid){
            $where['memberid']=$memberid;
        }
       if($where){
           $data=Db::name('member_account')->where($where)->select();
       }
        $order_type=account::$orderType;
        $this->assign('account',$data);
      //  dump($data);exit();
        $this->assign('order_type',$order_type);
        $this->assign('type',[1=>'普通账户',2=>'信用额度']);
       return $this->fetch('member_accounting');
    }

    //查询会员列表
    public function memberlist(){
        $serach=input("q");
        $where['name']=['like',"%$serach%"];
        $data=Db::name("member")->field('id,name')->where($where)->select();
        $member = [];
        $a= 0;
        foreach($data as $value){
            $b = $a++;
            $key=$value['id'].'-'.$value['name'];
            $member[$b]['id']=$key;
            $member[$b]['name']=$value['name'];
        }
        return json(['items'=>$member,'total_count'=>sizeof($member)]);
    }
    //批量审核
    public function examine(){
        $ids   = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        $table = 'member_withdrawals';
        $field = 'status';
        if (empty($ids)) return $this->error('缺少主键');
        $pk = Db::name($table)->getPk(); // 主键名称
        $map[$pk] = ['in', $ids];
        $result = Db::name($table)->where($map)->setField($field, 1);
        if (false !== $result) {
            Cache::clear();
            // 记录行为日志
            if (!empty($record)) {
                call_user_func_array('action_log', $record);
            }
            return $this->success('操作成功');
        } else {
            return $this->error('操作失败');
        }
    }
}